package com.ruoyi.quartz.task;

import com.ruoyi.worker.domain.TermWorker;
import com.ruoyi.worker.domain.TermWorkerExcel;
import com.ruoyi.worker.mapper.TermWorkerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述:长期工离职状态  转正时间  自动实现
 *
 * @author 刘松山
 * @date 2021/12/28 10:12
 */
@Component("termWorkerTask")
public class TermWorkerTask {
    @Autowired
    private TermWorkerMapper termWorkerMapper;

    private static final Logger LOGGER = LoggerFactory.getLogger(TermWorkerTask.class);

    public void executeJob() {
        LOGGER.info("长期工自动修改离职状态、转正日期开始==================");
        TermWorker t = new TermWorker();
        //获取离职时间在今天的长期工记录
        t.setLeaveTime(new Date());
        List<TermWorkerExcel> quitList = termWorkerMapper.selectTermWorkerTask(t);
        for (TermWorkerExcel termWorkerExcel : quitList) {
            termWorkerExcel.setWorkStatus(0);
        }


        //获取还没有转正的长期工记录
        t.setLeaveTime(null);
        t.setRegularTime(new Date());
        List<TermWorkerExcel> toRegularList = termWorkerMapper.selectTermWorkerTask(t);

        List<String> list = new ArrayList<>();
        for (TermWorkerExcel e : toRegularList) {
            list.add(e.getIdCard());
        }
        //查询长期工上班个数

        //规则如下
        //为空的时候，由系统判定产生，如果是人手动选择的以手动的为主，不再被覆盖。
        // 判定规则：无职级的人员，当职级为空时，三个预设值按考勤结果分别满足3个通班个数、再7个通班个数变化，
        // 即无职级的人上满了11个通班的日期即为转正日期。

        List<TermWorkerExcel> regularList = termWorkerMapper.listRegulaTermWorker(list);

        // 修改离职时间，修改转正日期
        List<TermWorkerExcel> resultList = new ArrayList<>();
        resultList.addAll(quitList);
        resultList.addAll(regularList);
        if (resultList.size() > 0) {
            termWorkerMapper.updateTermWorkerList(resultList);
        }

        LOGGER.info("长期工自动修改离职状态、转正日期结束==================");

    }
}
